package cn.com.backstage.controller.base;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.ApiU_message;
import cn.com.api.service.ApiU_messageService;
import cn.com.util.*;

public class U_messageBaseController {

	private static Logger logger = Logger.getLogger(U_messageBaseController.class);

	@Autowired
	private ApiU_messageService apiU_messageService;

	/**
	 * 列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "u_message-list")
	public ModelAndView u_message_list(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/u_message_list.jsp");

		List<ApiU_message> u_messages = apiU_messageService.findU_messages();
		mv.addObject("u_messages", u_messages);

		return mv;
	}

	/**
	 * 进入新增或者编辑页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "u_message-add-pre")
	public ModelAndView u_message_add_pre(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/u_message_add.jsp");
		//
		if (request.getParameter("id") != null) {
			String id = request.getParameter("id");
			ApiU_message u_message = apiU_messageService.findU_messageById(Integer.valueOf(id));
			mv.addObject("u_message", u_message);
		}
		return mv;
	}

	/**
	 * 新增或者编辑
	 * 
	 * @return
	 */
	@RequestMapping(value = "u_message-add")
	public void u_message_add(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		String title = request.getParameter("title");
		String content = request.getParameter("content");
		Map<String, Object> map = new HashMap<String, Object>();
		// add
		if (id == null || (id.trim()).equals("")) {
			ApiU_message u_message = new ApiU_message();
			u_message.setConn_id(0);
			u_message.setType(0);
			u_message.setAdd_time(DateUtil.nowTimeToString());
			u_message.setIs_read(0);
			u_message.setRead_time("");
			u_message.setTitle(title);
			u_message.setContent(content);
			apiU_messageService.U_messageInsertion(u_message);
		} else {// edit
			ApiU_message u_message = apiU_messageService.findU_messageById(Integer.valueOf(id));
			u_message.setTitle(title);
			u_message.setContent(content);
			apiU_messageService.updateU_message(u_message);
		}
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "添加成功！");
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 删除
	 * 
	 * @return
	 */
	@RequestMapping(value = "u_message-delete")
	public void u_message_delete(HttpServletRequest request, HttpServletResponse response) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");

		// delete
		int ret_i = apiU_messageService.deleteU_message(Integer.valueOf(id));

		//
		if (ret_i > 0) {
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "删除成功！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

}
